package com.Q08;

import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0.0
 * 创建时间：2022/7/26 09:01
 */
public class Q08_04 {
    List<List<Integer>> res;
    public List<List<Integer>> subsets(int[] nums) {
        res = new ArrayList<>();
        helper(nums,0,new ArrayList<>());
        return res;
    }

    public void helper(int[] nums,int cur,List<Integer> path){
        if(cur==nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        helper(nums,cur+1,path);
        path.add(nums[cur]);
        helper(nums,cur+1,path);
        path.remove(path.size()-1);
    }

    private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int start) {
        list.add(new ArrayList<>(tempList));
        for (int i = start; i < nums.length; i++) {
            //  想象成一颗多叉树的遍历，遍历的节点从start开始
            tempList.add(nums[i]);
            backtrack(list,tempList,nums,i+1);
            tempList.remove(tempList.size()-1);
        }
    }

    public ArrayList<ArrayList<Integer>> test(int[] nums){
        ArrayList res = new ArrayList<>();
        backtrack(res,new ArrayList<>(),nums,0);
        return res;
    }

    public static void main(String[] args) {
        new Q08_04().subsets(new int[]{1,2,3}).forEach(p -> {
            p.forEach(q -> System.out.print(q.intValue()+" "));
            System.out.println();
        });
        System.out.println("----------");
        new Q08_04().test(new int[]{1,2,3}).forEach(p -> {
            p.forEach(q -> System.out.print(q.intValue()+" "));
            System.out.println();
        });;
    }
}
